﻿using System;
using System.Text;
using System.Data;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Base;

namespace HRM
{
    public partial class FormDependent : FormBaseDataGrid
    {
        private Button btnok;
        private Label lblstatus;
        private Button btncancel;

        private Ctrl_General mCtrlGeneral;
        private DataSet mdsDependent;
        private DataGridViewTextBoxColumn ID_Emp;
        private DataGridViewTextBoxColumn Emp_NameVN;
        private DataGridViewTextBoxColumn ID_Dependent;
        private DataGridViewTextBoxColumn NameOfDependent;
        private DataGridViewTextBoxColumn PIT_Code;
        private DataGridViewComboBoxColumn Gender;
        private CustomDataGridView.CalendarColumn DOB;
        private DataGridViewTextBoxColumn Card_ID;
        private DataGridViewTextBoxColumn Realationship;
        private DataGridViewTextBoxColumn Occupation;
        private DataGridViewTextBoxColumn Wrk_Place;
        private CustomDataGridView.CalendarColumn Begin_Date;
        private CustomDataGridView.CalendarColumn End_Date;
        private DataGridViewTextBoxColumn CreatedUser;
        private DataGridViewTextBoxColumn UpdatedUser;
        private CustomDataGridView.CalendarColumn CreatedDate;
        private CustomDataGridView.CalendarColumn UpdatedDate;
        private SqlDataAdapter mdaBDependentUpdate;

        private void InitializeComponent()
        {
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle();
            this.btnok = new System.Windows.Forms.Button();
            this.lblstatus = new System.Windows.Forms.Label();
            this.btncancel = new System.Windows.Forms.Button();
            this.ID_Emp = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Emp_NameVN = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.ID_Dependent = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.NameOfDependent = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.PIT_Code = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Gender = new System.Windows.Forms.DataGridViewComboBoxColumn();
            this.DOB = new CustomDataGridView.CalendarColumn();
            this.Card_ID = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Realationship = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Occupation = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Wrk_Place = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Begin_Date = new CustomDataGridView.CalendarColumn();
            this.End_Date = new CustomDataGridView.CalendarColumn();
            this.CreatedUser = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.UpdatedUser = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.CreatedDate = new CustomDataGridView.CalendarColumn();
            this.UpdatedDate = new CustomDataGridView.CalendarColumn();
            ((System.ComponentModel.ISupportInitialize)(this.mdbg_dataGrid)).BeginInit();
            this.SuspendLayout();
            // 
            // mdbg_dataGrid
            // 
            this.mdbg_dataGrid.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.mdbg_dataGrid.ColumnHeadersHeight = 60;
            this.mdbg_dataGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.ID_Emp,
            this.Emp_NameVN,
            this.ID_Dependent,
            this.NameOfDependent,
            this.PIT_Code,
            this.Gender,
            this.DOB,
            this.Card_ID,
            this.Realationship,
            this.Occupation,
            this.Wrk_Place,
            this.Begin_Date,
            this.End_Date,
            this.CreatedUser,
            this.UpdatedUser,
            this.CreatedDate,
            this.UpdatedDate});
            this.mdbg_dataGrid.Location = new System.Drawing.Point(8, 8);
            this.mdbg_dataGrid.Size = new System.Drawing.Size(754, 347);
            this.mdbg_dataGrid.KeyUp += new System.Windows.Forms.KeyEventHandler(this.mdbg_dataGrid_KeyUp);
            // 
            // btnok
            // 
            this.btnok.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btnok.Location = new System.Drawing.Point(641, 359);
            this.btnok.Name = "btnok";
            this.btnok.Size = new System.Drawing.Size(58, 23);
            this.btnok.TabIndex = 62;
            this.btnok.Text = "Save";
            this.btnok.Click += new System.EventHandler(this.btnok_Click);
            // 
            // lblstatus
            // 
            this.lblstatus.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.lblstatus.Location = new System.Drawing.Point(8, 359);
            this.lblstatus.Name = "lblstatus";
            this.lblstatus.Size = new System.Drawing.Size(200, 20);
            this.lblstatus.TabIndex = 61;
            // 
            // btncancel
            // 
            this.btncancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btncancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btncancel.Location = new System.Drawing.Point(703, 359);
            this.btncancel.Name = "btncancel";
            this.btncancel.Size = new System.Drawing.Size(58, 23);
            this.btncancel.TabIndex = 60;
            this.btncancel.Text = "Cancel";
            this.btncancel.Click += new System.EventHandler(this.btncancel_Click);
            // 
            // ID_Emp
            // 
            this.ID_Emp.DataPropertyName = "ID_Emp";
            this.ID_Emp.HeaderText = "Employee ID.";
            this.ID_Emp.MaxInputLength = 20;
            this.ID_Emp.Name = "ID_Emp";
            // 
            // Emp_NameVN
            // 
            this.Emp_NameVN.DataPropertyName = "Emp_NameVN";
            dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(163)));
            this.Emp_NameVN.DefaultCellStyle = dataGridViewCellStyle1;
            this.Emp_NameVN.HeaderText = "Full Name";
            this.Emp_NameVN.MaxInputLength = 50;
            this.Emp_NameVN.Name = "Emp_NameVN";
            this.Emp_NameVN.Width = 180;
            // 
            // ID_Dependent
            // 
            this.ID_Dependent.DataPropertyName = "ID_Dependent";
            this.ID_Dependent.HeaderText = "ID_Dependent";
            this.ID_Dependent.MaxInputLength = 4;
            this.ID_Dependent.Name = "ID_Dependent";
            this.ID_Dependent.Visible = false;
            // 
            // NameOfDependent
            // 
            this.NameOfDependent.DataPropertyName = "NameOfDependent";
            this.NameOfDependent.HeaderText = "Name Of Dependent";
            this.NameOfDependent.MaxInputLength = 50;
            this.NameOfDependent.Name = "NameOfDependent";
            this.NameOfDependent.Width = 150;
            // 
            // PIT_Code
            // 
            this.PIT_Code.DataPropertyName = "PIT_Code";
            this.PIT_Code.HeaderText = "PIT. Code";
            this.PIT_Code.MaxInputLength = 20;
            this.PIT_Code.Name = "PIT_Code";
            // 
            // Gender
            // 
            this.Gender.DataPropertyName = "Gender";
            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            this.Gender.DefaultCellStyle = dataGridViewCellStyle2;
            this.Gender.HeaderText = "Gender";
            this.Gender.Items.AddRange(new object[] {
            "Male",
            "Female"});
            this.Gender.MaxDropDownItems = 2;
            this.Gender.Name = "Gender";
            this.Gender.Width = 80;
            // 
            // DOB
            // 
            this.DOB.DataPropertyName = "DOB";
            dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            this.DOB.DefaultCellStyle = dataGridViewCellStyle3;
            this.DOB.HeaderText = "Day Of Birth";
            this.DOB.Name = "DOB";
            this.DOB.Width = 80;
            // 
            // Card_ID
            // 
            this.Card_ID.DataPropertyName = "Card_ID";
            this.Card_ID.HeaderText = "Card ID./Passport";
            this.Card_ID.MaxInputLength = 20;
            this.Card_ID.Name = "Card_ID";
            this.Card_ID.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.Card_ID.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
            // 
            // Realationship
            // 
            this.Realationship.DataPropertyName = "Realationship";
            this.Realationship.HeaderText = "Realationship";
            this.Realationship.MaxInputLength = 30;
            this.Realationship.Name = "Realationship";
            // 
            // Occupation
            // 
            this.Occupation.DataPropertyName = "Occupation";
            this.Occupation.HeaderText = "Occupation";
            this.Occupation.MaxInputLength = 100;
            this.Occupation.Name = "Occupation";
            this.Occupation.Width = 150;
            // 
            // Wrk_Place
            // 
            this.Wrk_Place.DataPropertyName = "Wrk_Place";
            this.Wrk_Place.HeaderText = "Place Of Working";
            this.Wrk_Place.MaxInputLength = 250;
            this.Wrk_Place.Name = "Wrk_Place";
            this.Wrk_Place.Width = 150;
            // 
            // Begin_Date
            // 
            this.Begin_Date.DataPropertyName = "Begin_Date";
            dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            dataGridViewCellStyle4.Format = "d";
            dataGridViewCellStyle4.NullValue = null;
            this.Begin_Date.DefaultCellStyle = dataGridViewCellStyle4;
            this.Begin_Date.HeaderText = "Starting Date";
            this.Begin_Date.Name = "Begin_Date";
            this.Begin_Date.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.Begin_Date.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.Begin_Date.Width = 80;
            // 
            // End_Date
            // 
            this.End_Date.DataPropertyName = "End_Date";
            dataGridViewCellStyle5.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            dataGridViewCellStyle5.Format = "d";
            this.End_Date.DefaultCellStyle = dataGridViewCellStyle5;
            this.End_Date.HeaderText = "End Date";
            this.End_Date.Name = "End_Date";
            this.End_Date.Width = 80;
            // 
            // CreatedUser
            // 
            this.CreatedUser.DataPropertyName = "CreatedUser";
            this.CreatedUser.HeaderText = "CreatedUser";
            this.CreatedUser.MaxInputLength = 20;
            this.CreatedUser.Name = "CreatedUser";
            this.CreatedUser.Visible = false;
            // 
            // UpdatedUser
            // 
            this.UpdatedUser.DataPropertyName = "UpdatedUser";
            this.UpdatedUser.HeaderText = "UpdatedUser";
            this.UpdatedUser.MaxInputLength = 20;
            this.UpdatedUser.Name = "UpdatedUser";
            this.UpdatedUser.Visible = false;
            // 
            // CreatedDate
            // 
            this.CreatedDate.DataPropertyName = "CreatedDate";
            this.CreatedDate.HeaderText = "CreatedDate";
            this.CreatedDate.Name = "CreatedDate";
            this.CreatedDate.Visible = false;
            // 
            // UpdatedDate
            // 
            this.UpdatedDate.DataPropertyName = "UpdatedDate";
            this.UpdatedDate.HeaderText = "UpdatedDate";
            this.UpdatedDate.Name = "UpdatedDate";
            this.UpdatedDate.Visible = false;
            // 
            // FormDependent
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.ClientSize = new System.Drawing.Size(769, 385);
            this.Controls.Add(this.btnok);
            this.Controls.Add(this.lblstatus);
            this.Controls.Add(this.btncancel);
            this.KeyPreview = true;
            this.Name = "FormDependent";
            this.TabText = "Dependents Management";
            this.Text = "Dependents Management";
            this.Activated += new System.EventHandler(this.BaseForm_Activated);
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.BasicForm_Closing);
            this.Load += new System.EventHandler(this.BasicForm_Load);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.BaseForm_KeyDown);
            this.Controls.SetChildIndex(this.mdbg_dataGrid, 0);
            this.Controls.SetChildIndex(this.btncancel, 0);
            this.Controls.SetChildIndex(this.lblstatus, 0);
            this.Controls.SetChildIndex(this.btnok, 0);
            ((System.ComponentModel.ISupportInitialize)(this.mdbg_dataGrid)).EndInit();
            this.ResumeLayout(false);

        }

        public FormDependent()
        {
            InitializeComponent();
        }

        protected override void BasicForm_Load(object sender, EventArgs e)
        {
            base.BasicForm_Load(sender, e);
        }

        protected override void BaseForm_Activated(object sender, System.EventArgs e)
        {
            base.BaseForm_Activated(sender, e);
        }

        protected override void BaseForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            base.BaseForm_KeyDown(sender, e);
        }

        protected override void BasicForm_Closing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            base.BasicForm_Closing(sender, e);
        }

        #region Load Master Data
        protected override void LoadMasterData()
        {
            try
            {
                btnCancel = btncancel;
                btnOk = btnok;
                lblStatus = lblstatus;
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot load Dependents Management Form'. Error: '" + ex.Message + "'", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
        #endregion

        #region Load Data
        private void LoadData()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                if (mCtrlGeneral == null) mCtrlGeneral = new Ctrl_General();

                string ls_SQL = "Select BT.ID_Emp, PF.Emp_NameVN, BT.ID_Dependent, BT.NameOfDependent, BT.PIT_Code, BT.Gender, BT.DOB, "
                    + " BT.Card_ID, BT.Realationship, BT.Occupation, BT.Wrk_Place, BT.Begin_Date, BT.End_Date, "
                    + " BT.CreatedUser, BT.UpdatedUser, BT.CreatedDate, BT.UpdatedDate "
                    + "From HRM_PersonnelDependent BT inner join HRM_PersonalProfile PF on BT.ID_Emp = PF.ID_Emp ";

                mdsDependent = mCtrlGeneral.GetSelect(ls_SQL, "HRM_PersonnelDependent");
                mCtrlGeneral.GetAllData(out mdaBDependentUpdate, "HRM_PersonnelDependent", "");

                if (mdsDependent == null) return;
                mdbg_dataGrid.DataSource = mdsDependent;
                mdbg_dataGrid.DataMember = "HRM_PersonnelDependent";

                mRowManager = (CurrencyManager)mdbg_dataGrid.BindingContext[mdsDependent, "HRM_PersonnelDependent"];
                // How to refresh the status of the row position?Ordinal_Number
                mRowManager.PositionChanged += new EventHandler(RowMaster_Changed);

                base.LoadMasterData();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Dependent Form'. Error: '" + ex.Message + "'", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Ctrl_LOG.Print("Dependent Form", "LoadData() method", "Error: " + ex.Message);
                Cursor = Cursors.Default;
            }
        }
        #endregion

        public override void save()
        {
            try
            {
                if (mdsDependent == null) return;
                mRowManager.EndCurrentEdit();
                if (!mdsDependent.HasChanges())
                {
                    base.save();
                    return;
                }
                DataSet ldsHasChanged = mdsDependent.GetChanges();
                foreach (DataRow dr in ldsHasChanged.Tables[0].Rows)
                {
                    if (dr.RowState == DataRowState.Added)
                    {
                        dr["CreatedDate"] = DateTime.Now;
                        dr["CreatedUser"] = mMainForm.ms_UserID;
                    }
                    else if (dr.RowState == DataRowState.Modified)
                    {
                        dr["UpdatedDate"] = DateTime.Now;
                        dr["UpdatedUser"] = mMainForm.ms_UserID;
                    }
                }
                string ls_ColName = "", ls_TableName = "";
                string ls_Error = mCtrlGeneral.Update(mdaBDependentUpdate, ldsHasChanged, "HRM_PersonnelDependent", out ls_ColName, out ls_TableName);
                if (ls_Error == "")
                {
                    mdsDependent.AcceptChanges();
                    int li_pos = 0;
                    if (GetCurrentAction() == MODES.MODIFY) li_pos = mRowManager.Position;
                    /*if(GetCurrentAction() == MODES.ADD)*/
                    LoadData();
                    if (li_pos > 0) mRowManager.Position = li_pos;
                    base.save();
                }
                else
                {
                    MessageBox.Show("Cannot save'. Error: '" + ls_Error + "'", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("DB Error: " + ex.Message, ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public override void cancel()
        {
            mdbg_dataGrid.Focus();
            if (mdsDependent == null) return;
            mRowManager.EndCurrentEdit();
            mdsDependent.RejectChanges();
            base.cancel();
        }

        public override void add()
        {
            mdbg_dataGrid.Focus();
            base.add();
        }

        public override void delete()
        {
            base.delete();
            if (!mb_AcceptDelete) return;
            DataSet ldsDelete = (DataSet)mdbg_dataGrid.DataSource;
            DataRow ldrDelete = ldsDelete.Tables[0].Rows[mRowManager.Position];
            ldrDelete.Delete();
            string lsColName = "", lsTableName = "";
            string lsReturnMessage = mCtrlGeneral.Update(mdaBDependentUpdate, ldsDelete, "HRM_PersonnelDependent", out lsColName, out lsTableName);
            if (lsReturnMessage != "")
            {
                MessageBox.Show("Cannot delete'. Error: '" + lsReturnMessage + "'", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                ldsDelete.RejectChanges();
                ldsDelete.AcceptChanges();
            }
            base.post_delete();
        }

        protected override void mdbg_dataGrid_KeyUp(object sender, KeyEventArgs e)
        {
            if (GetCurrentAction() == MODES.FULL || GetCurrentAction() == MODES.VOID) return;
            if (e.KeyCode != Keys.F9) return;
            OpenSelection(mdbg_dataGrid.Columns[mdbg_dataGrid.CurrentCell.ColumnIndex].Name);
        }

        #region Open selection
        public override void OpenSelection(string as_SelectioField)
        {
            SelectionParam lstr_param = new SelectionParam();
            lstr_param.ms_SelectionField = as_SelectioField;
            FormSelection lfrmSelect = new FormSelection();
            lfrmSelect.mfrmParentForm = this;
            lfrmSelect.Owner = mMainForm;

            switch (as_SelectioField)
            {
                case "ID_Emp":
                    lstr_param.ms_SelectionName = "Personnel Profile";
                    lfrmSelect.SetParam(lstr_param);
                    if (lfrmSelect.ShowDialog() == DialogResult.OK)
                    {
                        string[] astrID = lfrmSelect.GetParam().msa_ReturnValues[0].Split(',');
                        string[] astrName = lfrmSelect.GetParam().msa_ReturnValues[1].Split(',');
                        if (astrID.Length == 1)
                        {
                            mdbg_dataGrid["ID_Emp", mdbg_dataGrid.CurrentCell.RowIndex].Value = lfrmSelect.GetParam().msa_ReturnValues[0];
                            mdbg_dataGrid["Emp_NameVN", mdbg_dataGrid.CurrentCell.RowIndex].Value = lfrmSelect.GetParam().msa_ReturnValues[1];
                            mdbg_dataGrid["Begin_Date", mdbg_dataGrid.CurrentCell.RowIndex].Value = DateTime.Now;
                            mdbg_dataGrid["End_Date", mdbg_dataGrid.CurrentCell.RowIndex].Value = DateTime.Now;
                        }
                        else
                        {   // Assign first value for current row
                            mdbg_dataGrid["ID_Emp", mdbg_dataGrid.CurrentCell.RowIndex].Value = astrID[0];
                            mdbg_dataGrid["Emp_NameVN", mdbg_dataGrid.CurrentCell.RowIndex].Value = astrName[0];
                            mdbg_dataGrid["DOB", mdbg_dataGrid.CurrentCell.RowIndex].Value = DateTime.Now;
                            mdbg_dataGrid["End_Date", mdbg_dataGrid.CurrentCell.RowIndex].Value = DateTime.Now;
                            mdbg_dataGrid["Begin_Date", mdbg_dataGrid.CurrentCell.RowIndex].Value = DateTime.Now;
                            // Assign next value for new row
                            DataRow dr;
                            for (int i = 1; i < astrID.Length; i++)
                            {
                                dr = mdsDependent.Tables[0].NewRow();
                                dr["ID_Emp"] = astrID[i];
                                dr["Emp_NameVN"] = astrName[i];
                                dr["DOB"] = DateTime.Now;
                                dr["Begin_Date"] = DateTime.Now;
                                dr["End_Date"] = DateTime.Now;
                                mdsDependent.Tables[0].Rows.Add(dr);
                            }
                        }
                    } break;
            }
        }
        #endregion

        private void btnok_Click(object sender, System.EventArgs e)
        {
            save();
        }

        private void btncancel_Click(object sender, System.EventArgs e)
        {
            cancel();
        }

    }
}
